package com.netease.financial.data.net;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;

import com.baidu.autoupdatesdk.AppUpdateInfo;
import com.baidu.autoupdatesdk.AppUpdateInfoForInstall;
import com.baidu.autoupdatesdk.BDAutoUpdateSDK;
import com.baidu.autoupdatesdk.CPCheckUpdateCallback;
import com.netease.financial.common.annotation.NotProguard;
import com.netease.financial.common.util.NfsLog;
import com.netease.financial.data.entity.AppUpdateEntity;
import com.netease.financial.data.exception.AppUpdateInfoNotFoundException;

import retrofit.http.Query;
import rx.Observable;
import rx.Subscriber;

/**
 * Created by wyf on 11/13/15.
 */
@NotProguard
public class AppUpdateInfoServiceWrapper extends NfsRestApiServiceWrapper {

    private static final String TAG = AppUpdateInfoServiceWrapper.class.getSimpleName();

    private final NfsRestApiService mNfsRestApiService;
    private final Context mContext;

    public AppUpdateInfoServiceWrapper(Context context, NfsRestApiService nfsRestApiService) {
        super(nfsRestApiService);
        mNfsRestApiService = nfsRestApiService;
        mContext = context;
    }

    @Override
    public Observable<AppUpdateEntity> getAppUpdateInfo(@Query("device") String device, @Query("versionCode") int versionCode) {
        NfsLog.d(TAG, "baidu.getAppUpdateInfo");
        return getInternalAppUpdateInfo();
    }

    private Observable<AppUpdateEntity> getInternalAppUpdateInfo() {
        return Observable.create(new Observable.OnSubscribe<AppUpdateEntity>() {
            @Override
            public void call(final Subscriber<? super AppUpdateEntity> subscriber) {
                Looper.getMainLooper();
                Handler handler = new Handler(Looper.getMainLooper());
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        // BDAutoUpdateSDK.cpUpdateCheck接口需要在主线程中使用
                        BDAutoUpdateSDK.cpUpdateCheck(mContext, new CPCheckUpdateCallback() {
                            @Override
                            public void onCheckUpdateCallback(AppUpdateInfo appUpdateInfo, AppUpdateInfoForInstall appUpdateInfoForInstall) {
                                NfsLog.d(TAG, "baidu.onCheckUpdateCallback");
                                if (appUpdateInfo != null) {
                                    NfsLog.d(TAG, AppUpdateInfoServiceWrapper.this.transform(appUpdateInfo).toString());
                                    subscriber.onNext(AppUpdateInfoServiceWrapper.this.transform(appUpdateInfo));
                                    subscriber.onCompleted();
                                } else {
                                    subscriber.onError(new AppUpdateInfoNotFoundException("app update info not found via baidu sdk"));
                                }
                            }
                        });
                    }
                });

            }
        });
    }

    private AppUpdateEntity transform(AppUpdateInfo info) {
        AppUpdateEntity appUpdateEntity = new AppUpdateEntity();
        appUpdateEntity.setApkUrl(info.getAppUrl());
        appUpdateEntity.setAppName(info.getAppSname());
        appUpdateEntity.setChangeLog(info.getAppChangeLog());
        appUpdateEntity.setForceUpdate("NO");
        appUpdateEntity.setUpdateTips("应用更新");
        appUpdateEntity.setVersionCode(info.getAppVersionCode());
        appUpdateEntity.setVersionName(info.getAppVersionName());
        return appUpdateEntity;
    }

}
